package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.entity.ShopAccountPeriodEntity;
import com.yuanfeng.business.feign.UsermsServiceClient;
import com.yuanfeng.business.mapper.ShopAccountPeriodMapper;
import com.yuanfeng.business.service.ShopAccountPeriodService;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.shop.AccountPeriodVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class ShopAccountPeriodServiceImpl extends ServiceImpl<ShopAccountPeriodMapper, ShopAccountPeriodEntity>
    implements ShopAccountPeriodService{

    @Autowired
    private UsermsServiceClient usermsServiceClient;
    @Override
    public ResponseResult queryAccountByIds(UserTokenBeanDTO userTokenBeanDTO) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(userTokenBeanDTO.getToken());
        if (userInfo == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        String userId = userInfo.getUserId();
        boolean businessInfo = usermsServiceClient.isBusinessFegin(userId);
        //判断账户是否为企业子账号
        if (!businessInfo && userInfo.getIsEmployee().equals(CommonType.commonStatus.YES.getCode())) {
            //拿到主账号的id
            userId = userInfo.getCreateUser();
        }
        //查找账期
        AccountPeriodVO result = this.baseMapper.getAccountPeriodByIds(userTokenBeanDTO.getShopId(), userId);
        if (result != null && result.getIsAccount() == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail(BizCodeEnume.NOT_OPENED_ACCOUNT.getCode(), BizCodeEnume.NOT_OPENED_ACCOUNT.getMsg());
    }
}




